//
//  Common.m
//  models
//
//  Created by minfo on 13-3-4.
//  Copyright (c) 2013年 minfo. All rights reserved.
//

#import "Common.h"
#import "UIDevice+IdentifierAddition.h"

//#import "UIDevice+IdentifierAddition.h"

@implementation Common

- (NSMutableString*)GetWebAPI:(NSString*)FunName //手机请求端的 API
{
    NSMutableString* WebAPI=[NSMutableString stringWithString:@"http://116.228.55.5:8137/search/"];
    [WebAPI appendFormat:@"%@?",FunName];
    return WebAPI;
}
- (NSMutableString*)GetWebNewAPI:(NSString*)FunName //手机请求端的 API
{
    NSMutableString* WebAPI=[NSMutableString stringWithString:@"http://210.14.67.70/modelplus/"];
    [WebAPI appendFormat:@"%@?",FunName];
    return WebAPI;
}
+ (NSMutableString*)Coordinate:(NSString*)url //经纬度
{
    NSMutableString* coordinate=[NSMutableString stringWithString:url];
    [coordinate appendFormat:@"&imsi=%@",@"UnlimitedUser"];
     [coordinate appendFormat:@"&software_version=%@",@"IOS"];
     [coordinate appendFormat:@"&device_id=%@",[[Common alloc]GetDeviceID]];
    if ([[[[NSUserDefaults standardUserDefaults] objectForKey:@"Location"] objectForKey:@"gpsx"] floatValue]>0) {
    
       [coordinate appendFormat:@"&gps_x=%@",[[[NSUserDefaults standardUserDefaults] objectForKey:@"Location"] objectForKey:@"gpsx"]];
    }
    if ([[[[NSUserDefaults standardUserDefaults] objectForKey:@"Location"] objectForKey:@"gpsy"] floatValue]>0) {
        
        [coordinate appendFormat:@"&gps_y=%@",[[[NSUserDefaults standardUserDefaults] objectForKey:@"Location"] objectForKey:@"gpsy"]];
    }
    [coordinate appendFormat:@"&network_type_name=%@",@"wifi"];
    return coordinate;
}

- (int)Getlanguage //客户端语言，0为英文,1为中文
{
    return  1;
}

- (NSString*)GetDeviceID //手机设备唯一标识
{
    NSString *deviceUID=@"";
    deviceUID= [[UIDevice currentDevice] uniqueGlobalDeviceIdentifier];
//    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {
//        
//    }
//    else {
//        deviceUID =[[UIDevice currentDevice]uniqueIdentifier];
//    }
    return deviceUID;
}

- (NSString*)GetUserCoop //1.0
{
    return @"UnlimitedUser";
}

- (NSString*)GetRegsource//注册来源，客户端标识（sms，wap，invite,android，iphone，91iphone，jifengandroid。。。仅作为参考，不以此为准）
{
    return @"iphone";
}

- (NSString*)GetApipwd//接口密码
{
    return @"UnlimitedPassword";
}


- (NSString*)GetChannel//获取频道
{
    return @"QnsRobot";
}

- (NSString*)GetMobile//获取手机号
{
    return @"15921396176";
}

- (NSString*)GetLocation//获取城市
{
    NSString *str=@"上海";
    return [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
-(NSString*)GetEncode:(NSString*) str//获取UTF-8
{
    return [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

-(NSInteger)GetUserID //获取用户ID
{
    NSUserDefaults *use=[NSUserDefaults standardUserDefaults];
    return [use boolForKey:@"use"]?[[use objectForKey:@"UserID"] integerValue]:-1;
}
//获取昵称
-(NSString *)getAliasName{
    
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"AliasName"];
}
//获取Token
+(NSString *)getToken{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceToken"];
}
//写入token
+(void)SetToke:(NSString *)token{
      [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"deviceToken"];
}


+(void)SetCity:(NSString*)city
{
    [[NSUserDefaults standardUserDefaults] setObject:city forKey:@"city"];
}
//获取城市
+(NSString *)GetCity{
    NSUserDefaults *use=[NSUserDefaults standardUserDefaults];
    return [use objectForKey:@"city"]?[use objectForKey:@"city"]:@"上海";
}
//得到省份
+(void)SetProvince:(NSString*)province
{
    [[NSUserDefaults standardUserDefaults] setObject:province forKey:@"province"];
}
//获取省份
+(NSString *)GetProvince{
    NSUserDefaults *use=[NSUserDefaults standardUserDefaults];
    return [use objectForKey:@"province"]?[use objectForKey:@"province"]:@"";
}
//绑定手机
+(void)setMobil:(NSString *)number{
//    NSUserDefaults *use=[NSUserDefaults standardUserDefaults];
    [[NSUserDefaults standardUserDefaults] setObject:number forKey:@"phoneNumber"];
}
//获取手机
+(NSString *)GetPhone{
    NSUserDefaults *use=[NSUserDefaults standardUserDefaults];
    return [use objectForKey:@"phoneNumber"]?[use objectForKey:@"phoneNumber"]:@"";

}

//写入历史记录
+(void)SetHistorySearch:(NSMutableArray *)searchKey{
    [searchKey writeToFile:[self getHistoryPath] atomically:YES];
    
}
//获取历史记录
+(NSMutableArray *)GetHistorySearch{
    NSMutableArray *arr=[NSMutableArray arrayWithContentsOfFile:[self getHistoryPath]];
    return arr;
}
//通知信息
+(void)SetNotification:(NSString *)swich{

    [[NSUserDefaults standardUserDefaults] setObject:swich forKey:@"notification"];

}
//通知开关
+(NSString *)GetNotification{
    NSUserDefaults *use=[NSUserDefaults standardUserDefaults];
    return [use objectForKey:@"notification"]?[use objectForKey:@"notification"]:@"btnSwichOn";
}
//获取Bound
-(NSString *)getIsBound{

    return [[NSUserDefaults standardUserDefaults] objectForKey:@"IsBound"];
}
-(NSString*)GetUser{

    return [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
}
-(NSString*)GetSign{
    NSUserDefaults *use=[NSUserDefaults standardUserDefaults];
    return [use objectForKey:@"Sign"];
}
-(NSString*)GetAvatar{
    NSUserDefaults *use=[NSUserDefaults standardUserDefaults];
    return [use objectForKey:@"Avatar"];
}

-(NSString *)GetStartImg{
    NSUserDefaults *use=[NSUserDefaults standardUserDefaults];
    return [use objectForKey:@"StartImg"];
}
-(void)ShowMessage:(NSString*)Title Remark:(NSString*)Remark
{
    UIAlertView* showSelection = [[UIAlertView alloc]
                                  initWithTitle: Title
                                  message:Remark
                                  delegate: nil
                                  cancelButtonTitle: @"Ok"
                                  otherButtonTitles: nil];
    [showSelection show];
}



//获取文件路径方法
+ (NSString *)getHistoryPath {
    NSArray *segments = [NSArray arrayWithObjects:NSHomeDirectory(),@"Library",@"Caches",@"history.plist", nil];
    return [NSString pathWithComponents:segments];
}

//获取缓存图片
- (NSString *)getImgPath {
    
    NSArray *segments = [NSArray arrayWithObjects:NSHomeDirectory(),@"Library",@"Caches",@"image", nil];
    
    return [NSString pathWithComponents:segments];
    
}

-(void)SetUserinfo:(NSString*)UserName Password:(NSString*)Password UserID:(NSInteger)UserID Avatar:(NSString*)Avatar Gender:(NSString *)gender Sign:(NSString *)sign IsBound:(int)IsBound
{
    NSUserDefaults *use=[NSUserDefaults standardUserDefaults];
    [use setBool:YES forKey:@"use"];
    [use setObject:[NSString stringWithFormat:@"%d",UserID] forKey:@"UserID"];
    [use setObject:UserName forKey:@"UserName"];
//    [use setObject:UserName forKey:@"UserName"];
    [use setObject:Password forKey:@"Password"];
    [use setObject:Avatar forKey:@"Avatar"];
    [use setObject:gender forKey:@"Gender"];
    [use setObject:sign forKey:@"Sign"];
    [use setObject:[NSString stringWithFormat:@"%d",IsBound] forKey:@"IsBound"];
}

-(void)SetUserinfo:(NSString*)UserName Password:(NSString*)Password UserID:(NSInteger)UserID Avatar:(NSString*)Avatar Gender:(NSString *)gender Sign:(NSString *)sign
{
    [self SetUserinfo:UserName Password:Password UserID:UserID Avatar:Avatar Gender:gender Sign:sign IsBound:0];
}

-(void)SetUserinfo:(NSString*)UserName Password:(NSString*)Password UserID:(NSInteger)UserID
{
    [self SetUserinfo:UserName Password:Password UserID:UserID Avatar:@"" Gender:@"" Sign:@""];
}



-(void)SetUAvatar:(NSString*)Avatar
{
    NSUserDefaults *use=[NSUserDefaults standardUserDefaults];
    [use setObject:Avatar forKey:@"Avatar"];
}
-(void)SetStartImg:(NSString *)startImg{
    NSUserDefaults *use=[NSUserDefaults standardUserDefaults];
    [use setObject:startImg forKey:@"StartImg"];
}
- (BOOL)CheckLogin
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"use"];
}


- (BOOL)CheckInvite
{
    return [[[NSUserDefaults standardUserDefaults] objectForKey:@"IsBound"] integerValue]==0?NO:YES;
}

//利用正则表达式验证
-(BOOL)isCheckEmail:(NSString *)str {
    NSString *Regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    return [Test evaluateWithObject:str];
}

// 汉字验证(1-20个)
-(BOOL)isCheckChinese:(NSString *)str {
    NSString *Regex = @"^[\u4e00-\u9fa5]{1,20}+$";
    NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    return [Test evaluateWithObject:str];
}

// 纯英文(1-20个)
-(BOOL)isCheckLetters:(NSString *)str {
    NSString *Regex = @"^[A-Za-Z]{6,20}+$";
    NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    return [Test evaluateWithObject:str];
}

// 手机号
+(BOOL)isMobileCheck:(NSString *)str{
    NSString *Regex = @"^(13[0-9]|14[0-9]|15[0|1|2|3|5|6|7|8|9]|18[0|2|5|6|7|8|9])\\d{8}$";
    NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    return [Test evaluateWithObject:str];
}
//
//// 用户名检测 纯英文或者纯中文 10个字符
//+(BOOL)userNameCheck:(NSString *)str {
//    BOOL isReal = NO;
//    NSString *userChinese = @"^[\u4e00-\u9fa5]{1,5}+$";
//    isReal = [str isMatchedByRegex:userChinese];
//    if (!isReal) {
//        NSString *namStr = @"^[a-zA-Z]{1,10}+$";
//        isReal = [str isMatchedByRegex:namStr];
//    }
//    return isReal;  // YES 符合要求 NO 不符合
//}
//
-(BOOL)userIDNumCheck:(NSString *)IDNum{
//    @"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$
    NSString *num = @"^(^\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$";
//    NSString *num = @"^\\d{17}+[0-9a-zA-Z]$";
    NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", num];
    return [Test evaluateWithObject:IDNum];
}

//// 身份证
//+(BOOL)userIDNumCheck:(NSString *)IDNum{
//    NSString *num = @"^\\d{17}+[0-9a-zA-Z]$";
//    BOOL isID = [IDNum isMatchedByRegex:num];
//    
//    return isID;
//}
//
//// 邮箱验证
//+(BOOL)emailCheck:(NSString *)str{
//    NSString *email = @"^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$";
//    BOOL isEmail = [str isMatchedByRegex:email];
//    
//    return isEmail; // YES 是邮箱
//}
//
//// QQ号验证
//+(BOOL)qqNumCheck:(NSString *)str {
//    NSString *qqregex = @"^[1-9][0-9]{4,}$";
//    BOOL isQQ = [str isMatchedByRegex:qqregex];
//    
//    return isQQ;
//}
//
//// MSN号码验证
//+(BOOL)msnNumCheck:(NSString *)str {
//    BOOL isMSN;
//    NSRange ran = [str rangeOfString:@"@"];
//    if (ran.location == NSNotFound) {
//        isMSN = NO;
//    }
//    else {
//        NSArray  *enterArr= [str componentsSeparatedByString:@"@"];
//        NSString *hou = [enterArr objectAtIndex:1];
//        if ([hou isEqualToString:@"hotmail.com"] || [hou isEqualToString:@"live.com"]
//            || [hou isEqualToString:@"live.cn"] ) {
//            isMSN = YES;
//        }
//        else {
//            isMSN = NO;
//        }
//    }
//    return isMSN;
//}
//

//
//
//// 字符串是否 是纯回车,空格
//+(BOOL)stringIsEmptyNext:(NSString *)theString {
//    BOOL have = NO; // 不包含
//    NSString *empty = @"^[ ]{2,}$";
//    NSString *huiChe = @"^[\n]{2,}$";
//    if ([theString isMatchedByRegex:empty] || [theString isMatchedByRegex:huiChe]) {
//        return YES;
//    }
//    if ([theString isEqualToString:@" "] || [theString isEqualToString:@"\n"]) {
//        return YES;
//    }
//    return have;    // YES 包含回车或者空格
//}
//
//
//// 座机电话号码
//+(BOOL)callNumCheck:(NSString*)theString {
//    BOOL result = NO;
//    NSString *guDing = @"^[0-9]{3}-[0-9]{8}|[0-9]{4}-[0-9]{7}$";
//    BOOL gu = [theString isMatchedByRegex:guDing];
//    if (gu) {
//        result = YES;
//    }
//    else {
//        result = NO;
//    }
//    
//    return result;
//}
@end
